﻿@model Budgeter.ViewModels.Budget.CreateOrEdit.ViewModel
@{
    bool isNew = (Model.BudgetId <= 0);
    ViewBag.Title = isNew ? "Create a New Budget" : "Edit Your Budget";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/Budget/CreateOrEdit.js")" type="text/javascript"></script>
<script type="text/javascript">
    var intervals = @Html.Raw(Model.IntervalsAsJson)
</script>
@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.HiddenFor(model => model.ReceiptNumber)
    @Html.ValidationSummary(true)

    <div class="row">
        <div class="control-group span5">
            @Html.LabelFor(model => model.IntervalId)
            <div class="controls">
                @Html.DropDownListFor(
                    model => model.IntervalId,
                    Budgeter.ViewModels.ViewModelHelpers.GetIntervalSelectListItems(Model.AllIntervals),
                    "Select..."
                    )
                <div class="help-block">
                   @Html.ValidationMessageFor(model => model.IntervalId)
                </div>
            </div>
        </div>
    </div>
    
    <hr />

    var entriesByCategory = from e in Model.Entries
                            group e by e.Category into g
                            select new { Key = g.Key, Entries = g };
    int entryCounter = 0;
    var ee = entriesByCategory;

    <table class="">
        @foreach (var category in ee)
        {
            <tr><td colspan="7">
                <h3>@category.Key</h3>
            </td></tr>
                    
            foreach (var entry in category.Entries)
            {
                string prefix = string.Format("Entries[{0}]", entryCounter);
                entryCounter++;
                   
                <tr class="entry clearable @(entry.IsIncome ? "income" : "expense")">
                    <td class="span4">
                        @entry.Name
                        @Html.Hidden(String.Format("{0}.Category", prefix), entry.Category)
                        @Html.Hidden(String.Format("{0}.Name", prefix), entry.Name)
                        @Html.Hidden(String.Format("{0}.IsIncome", prefix), entry.IsIncome)
                    </td>
                    <td>
                        @Html.TextBox(String.Format("{0}.Amount", prefix), entry.Amount, new { @class = "span2" })

                        per

                        @Html.DropDownList(
                            String.Format("{0}.IntervalId", prefix),
                            Budgeter.ViewModels.ViewModelHelpers.GetIntervalSelectListItems(Model.AllIntervals, entry.IntervalId),
                            "Select...",
                            new { @class = "span2" })

                        <div class="help-block">
                            @Html.ValidationMessage(String.Format("{0}.Amount", prefix))
                            @Html.ValidationMessage(String.Format("{0}.Interval", prefix))
                        </div>
                    </td>
                    <td class="span2 translatedamountcontainer right">
                        <strong class="translatedamount">...</strong>
                        <small class="translatedinterval"></small>
                    </td>
                    <td class="span1 right">
                        <span class="percentage"></span>
                    </td>
                    <td class="span1 right">
                        <a class="clearlink" href="javascript:void()">(clear)</a>
                    </td>
                </tr>
            }
        }
    </table>

    <hr />

    <button class="btn btn-primary">@(isNew ? "Create" : "Save")</button>
}



@section TopBar
{
    <div class="topbarsummary well">
        <div class="name">
            Receipt:
        </div>
        <div class="receiptnumber pull-right">
            @if (Model.ReceiptNumber == 0)
            {
                    <i>New</i>
            }
            else
            {
                @Model.ReceiptNumber
            }
        </div>

        <div class="clear"></div>

        <div class="name">
            Total Income
        </div>
        <div id="totalincome" class="value">
        </div>

        <div class="clear"></div>

        <div class="name">
            Total Expense
        </div>
        <div id="totalexpense" class="value">
        </div>

        <div class="clear"></div>

        <div class="name">
            Surplus
        </div>
        <div id="surplus" class="value">
        </div>
    </div>
}
